home *** CD-ROM | disk | FTP | other *** search
- /* Complex or nested if and if-else statements */
- using System;
-
- namespace Chapter2 {
- class Class1 {
- static void Main() {
- string Input;
- float Age, Weight;
- int iQuickAnswer;
-
- // note: you'll want to hit return before entering the second answer
- Console.Write("How old are you and how much do you weigh? ");
- Input = Console.ReadLine();
- Age = float.Parse(Input);
- Input = Console.ReadLine();
- Weight = float.Parse(Input);
-
- if (Age < 3 || Weight < 35) { // start compound if statement
- Console.Write("\nThe law requires you to sit in a car seat\n"
- + "\n Do you have a car seat? ");
- iQuickAnswer = Console.Read();
-
- if (iQuickAnswer == 'y') // nested if statement
- Console.Write("\n Good, but using it would be better.\n");
- else { // nested else compounded
- Console.Write("\n No, do you care about your baby?\n");
- if (iQuickAnswer == 'y') // nested if in nested else
- Console.Write("\n Well then get a car seat!\n");
- else if (iQuickAnswer == 'n') // nested else-if in nested else
- Console.Write("\n You sicken me!\n"
- + "\n Your baby is worth it!\n");
- else // nested else in nested else
- Console.Write("\n Your baby is worth it!\n");
- }
- } // end compound if statement
- else if (Age > 85 || Weight > 500)
- Console.WriteLine("\n Sorry I asked!\n");
- }
- }
- }
-
-